<template>
    <div>
      <span v-for="item in cartlist">
          商品名称：{{item.username}}<br>
          商品数量：{{item.num}}<br>
          商品价格小计：{{item.totalprice}}<br>
          <br>
      </span>

      你的优惠卷：<br>
      <span v-for="i in juanlist">
          {{i.username}} <button @click="sy(i.id)">使用优惠卷</button><br>
      </span>
      总价格：{{total}}
    </div>
</template>

<script>
  import axios from 'axios'
    export default {
        name: "Cart",
      data(){
          return{
            cartlist:[],
            juanlist:[],
            total:0
          }
      },
      mounted() {
          let from_data = new FormData();
          from_data.append('uid',localStorage.getItem('uid'));
          axios({
            url:'http://127.0.0.1:8000/cart/',
            method:'post',
            data:from_data

          }).then(res=>{
            this.cartlist = res.data.cartdata
            this.juanlist = res.data.juandata
            localStorage.setItem('totalprice',res.data.price)

          })
          this.total = localStorage.getItem('totalprice')
      },
      methods: {
        sy(id){
            let from_data = new FormData();
            from_data.append('uid',localStorage.getItem('uid'))
            from_data.append('jid',id)
            axios({
            url:'http://127.0.0.1:8000/juan/',
            method:'post',
            data:from_data
        }).then(res=>{
            if(res.data.bm == 105){
                this.total = this.total - 5
                localStorage.setItem('totalprice','total')
                window.location.reload()
            }
            else(res.data.bm == 2010)
                this.total = this.total - 10
                localStorage.setItem('totalprice','total')

        })
        }
    },
    }
</script>

<style scoped>

</style>
